package org.example.cloud.order.controller;

import org.example.cloud.order.feign.PaymentFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    private static final String serviceName="payment";

    @Autowired
    private PaymentFeign paymentFeign;

    /**
     * 测试 ribbon
     */
    @GetMapping
    public void printOrder(){
        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://"+serviceName+"/payment", String.class);

        System.out.println(forEntity.getBody());
    }

    /**
     * 测试feign(feign基于ribbon,配置ribbon会影响feign,比如调度策略)
     */
    @GetMapping("/2")
    public void printOrder2(){
        String payment = paymentFeign.payment();

        System.out.println("feign"+payment);
    }

    /**
     * 测试feign远程调用，当被调用服务发生错误时或者被调用发生服务降级情况
     *
     * 结果：完全按照被调用的（被处理）返回结果返回
     *      a.被调用服务降级，那么按照降级结果多半是空
     *      b.被调用服务发生异常，那么直接抛出异常
     *      c.被调用超时，其实feign会发生重试，默认重试一次，需要保证幂等性
     */
    @GetMapping("/3")
    public void printOrder3(){
        String payment = paymentFeign.payment2();

        System.out.println("feign"+payment);
    }

    @GetMapping("/zipkin")
    public void zipkin(){
        String zipkin = paymentFeign.zipkin();
        System.out.println("feign"+zipkin);
    }

    @GetMapping("/zipkin/timeout")
    public void zipkinTimeout(){
        String zipkin = paymentFeign.zipkinTimeout();
        System.out.println("feign"+zipkin);
    }
}
